package org.springblade.modules.repair.vo.plan;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springblade.common.cache.DictCache;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.repair.entity.MaintainTemplate;
import org.springblade.modules.repair.enums.MaintainPlanContentEnum;
import org.springblade.modules.repair.enums.TaskStatusEnum;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @Description 首页-我的任务分页
 * @Author ysq
 * @Date 2024/1/11
 */
@Data
public class PlanOrderHomePageVO {

	private Long id;

	@ApiModelProperty(value = "电梯id")
	private Long elevatorId;

	@ApiModelProperty(value = "工单类型 1: 保养, 2: 维修, 3: 困人, 4: 配件, 5: 事务, 6: 质检")
	private Integer typeStatus;

	@ApiModelProperty(value = "工单类型名称")
	private String typeStatusName;

	@JsonFormat(pattern = "yyyy-MM-dd")
	@ApiModelProperty(value = "开始时间")
	private Date planDate;

	@ApiModelProperty(value = "结束时间")
	private Date finishDate;

	@ApiModelProperty(value = "0: 未延迟, 1: 延迟")
	private Integer isDeferred;

	@ApiModelProperty(value = "状态")
	private Integer status;

	@ApiModelProperty(value = "状态名称")
	private String statusName;

	@ApiModelProperty(value = "工单更新时间")
	private Date updateTime;

	@ApiModelProperty(value = "工单创建时间")
	private Date createTime;

	private String workUserInfos;

	@ApiModelProperty(value = "工单人员信息")
	private LinkedList<PlanOrderHomeUserVO> workUserInfoList;

	@ApiModelProperty(value = "项目id")
	private Long buildingId;

	@ApiModelProperty(value = "项目名称")
	private String buildingName;

	@ApiModelProperty(value = "项目纬度")
	private BigDecimal latitude;

	@ApiModelProperty(value = "项目经度")
	private BigDecimal longitude;

	@ApiModelProperty(value = "项目别名，多个逗号隔开")
	private String aliasStr;

	@ApiModelProperty(value = "设备代码")
	private String privateNum;

	@ApiModelProperty(value = "品牌名称")
	private String brandName;

	@ApiModelProperty(value = "电梯位置")
	private String elevatorAddress;

	@ApiModelProperty("紧急程度 1:非常紧急 2:紧急 3:普通 4:较低")
	private Integer emerProcType;

	/**
	 * 保养详情
	 */
	@ApiModelProperty("保养名称")
	private String planCheckName;

	@ApiModelProperty("停梯状态： 1-正常 2-停梯")
	private Integer elevatorIsStop;

	@ApiModelProperty("逾期文案")
	private String overdueName;

	@ApiModelProperty("1:非年审，2:年审")
	private Integer auditType;

	@ApiModelProperty("电梯年审状态1:非年审，0:年审")
	private Integer elevatorAuditType;

	@ApiModelProperty("超时文案")
	private String timeliness;

	@ApiModelProperty("保养模板")
	List<MaintainTemplate> maintainTemplates;

	@ApiModelProperty("保养模板名称")
	private String templateName;

	/**
	 * 维修详情
	 */
	@ApiModelProperty("维修故障原因")
	private List<String>  problemItemNames;
	private String problemItem;

	/**
	 * 救援详情
	 */
	@ApiModelProperty(value = "报警类型 0-困人，1-伤人")
	private Integer trapType;

	@ApiModelProperty(value = "报警备注")
	private String trapDesc;

	@ApiModelProperty(value = "服务器时间")
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date serviceDate;

	/**
	 * 配件详情
	 */
	@ApiModelProperty(value = "部件名称集合")
	private String partsName;

	@ApiModelProperty(value = "部件数量")
	private Integer partsNum;

	/**
	 * 事务详情
	 */
	@ApiModelProperty("事务工单的描述信息")
	private String affairDesc;

	/**
	 * 质检详情
	 */
	@ApiModelProperty(value = "1:保养质检，2:重复故障质检，3:维保移交质检，4:安装移交质检，5:常规质检")
	private Integer checkType;

	public List<PlanOrderHomeUserVO> getWorkUserInfoList() {
		if (Func.isBlank(this.workUserInfos)) {
			return workUserInfoList;
		}

		this.workUserInfoList = new LinkedList<>();
		String[] userArr = Func.split(this.workUserInfos, ",");
		for (String user : userArr) {
			PlanOrderHomeUserVO planOrderHomeUser = new PlanOrderHomeUserVO();
			String[] userInfoArr = Func.split(user, "|");
			planOrderHomeUser.setUserId(Long.valueOf(userInfoArr[0]));
			planOrderHomeUser.setUserName(userInfoArr[1]);
			planOrderHomeUser.setIsPlanStatus(Boolean.valueOf(userInfoArr[2]));
			this.workUserInfoList.add(planOrderHomeUser);
		}
		return workUserInfoList;
	}

	public String getStatusName() {
		return TaskStatusEnum.getTaskName(this.typeStatus, this.status);
	}

	public String getTypeStatusName() {
		return MaintainPlanContentEnum.getName(this.typeStatus);
	}

	public void setPartsName(String partsName) {
		if(Func.isNotEmpty(partsName)){
			this.partsName = partsName.replace("&amp;&amp;",">");
		}
	}

	public void setProblemItem(String problemItem) {
		if (Func.isNotBlank(problemItem)) {
			List<String> problemKeys = Func.toStrList(problemItem);
			List<String> problemNames = new ArrayList<>();
			for (String problemKey : problemKeys) {
				String problemName = DictCache.getValue("fault_problem", problemKey);
				problemNames.add(problemName);
			}
			this.problemItemNames = problemNames;
		}
		this.problemItem = problemItem;
	}
}
